{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "l=[9,8,7,5,0,2]\n",
    "def test():\n",
    "    l.sort()\n",
    "    return\n",
    "test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-11-91e021cb008f>, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-11-91e021cb008f>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m    show('root',age=22,'男')\u001b[0m\n\u001b[1;37m                      ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "def show(name,age,sex):\n",
    "    print(\"Hello,your name is %s,age is %s,sex is %s\"%(name,age,sex))\n",
    "\n",
    "show('root',age=22,'男')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " type(hex(-42))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140712733680304\n",
      "140712733680304\n",
      "140712733680304\n",
      "140712733680304\n",
      "140712733680304\n",
      "140712733680304\n",
      "140712733680304\n",
      "140712733680304\n",
      "140712733680304\n",
      "140712733680304\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 10\n",
    "\n",
    "def change(x):\n",
    "    for i in range(10):\n",
    "        x = i\n",
    "        print(id(a))\n",
    "\n",
    "change(a)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1次循环\n",
      "{'age': 1}\n",
      "##########\n",
      "[]\n",
      "##########\n",
      "[{'age': 1}]\n",
      "第2次循环\n",
      "{'age': 2}\n",
      "##########\n",
      "[{'age': 1}]\n",
      "##########\n",
      "[{'age': 1}, {'age': 2}]\n"
     ]
    }
   ],
   "source": [
    "l = []\n",
    "def add_value():\n",
    "    for i in range(1, 3):\n",
    "        d = {}\n",
    "        print('第%s次循环' %i)\n",
    "        d['age'] = i\n",
    "        print(d)\n",
    "        print('#' * 10)\n",
    "        print(l)\n",
    "        print('#' * 10)\n",
    "        l.append(d)\n",
    "        print(l)\n",
    "add_value()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0b10'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'100.00100'"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def tobin(num):\n",
    "    if num < 0:\n",
    "        print('暂不支持负数转化')\n",
    "        return\n",
    "    integer = int(num)\n",
    "    decimal = num - integer\n",
    "    if integer==0:\n",
    "        integerStr = ''\n",
    "    else:\n",
    "        integerStr = integerPart(integer,2)\n",
    "    if decimal==0:\n",
    "        decimalStr = ''\n",
    "    else:\n",
    "        decimalStr = decimalPart(decimal,2,5)\n",
    "    return integerStr + \".\" + decimalStr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tohex(num):\n",
    "    if num < 0:\n",
    "        print('暂不支持负数转化')\n",
    "        return\n",
    "    integer = int(num)\n",
    "    decimal = num - integer\n",
    "    if integer==0:\n",
    "        integerStr = ''\n",
    "    else:\n",
    "        integerStr = integerPart(integer,16)\n",
    "    if decimal==0:\n",
    "        decimalStr = ''\n",
    "    else:\n",
    "        decimalStr = decimalPart(decimal,16,5)\n",
    "    return integerStr + \".\" + decimalStr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'10'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算整数部分\n",
    "def integerPart(integer,base):\n",
    "    strList = ['A','B','C','D','E','F']\n",
    "    if integer == 0:\n",
    "        return ''\n",
    "    # 算出当前可添加的数和下一次递归要用的数\n",
    "    thisNum = integer%base\n",
    "    nextNum = integer//base\n",
    "    if thisNum > 9:\n",
    "        return integerPart(nextNum,base) + strList[thisNum%10]\n",
    "    return integerPart(nextNum,base) + str(thisNum)\n",
    "        \n",
    "integerPart(4,2)\n",
    "integerPart(2,2)\n",
    "integerPart(2,16)\n",
    "integerPart(2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'00100'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算小数部分\n",
    "def decimalPart(decimal,base,digit):\n",
    "    strList = ['A','B','C','D','E','F']\n",
    "    if digit ==0 or decimal == 0:\n",
    "        return ''\n",
    "    # 用digit来控制递归次数\n",
    "    digit-=1\n",
    "    # 算出当前可添加的数和下一次递归要用的数\n",
    "    thisNum = int(decimal*base)\n",
    "    nextNum = decimal*base-thisNum\n",
    "    if thisNum > 9:\n",
    "        return strList[thisNum%10] + decimalPart(nextNum,base,digit)\n",
    "    return str(thisNum) + decimalPart(nextNum,base,digit)\n",
    "decimalPart(0.13337777333330033,2,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'integerPart' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-5a3ba7e10001>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     26\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mintegerStr\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m\".\"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mdecimalStr\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     27\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 28\u001b[1;33m \u001b[0mconvert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-1-5a3ba7e10001>\u001b[0m in \u001b[0;36mconvert\u001b[1;34m(num, base, digit)\u001b[0m\n\u001b[0;32m     19\u001b[0m         \u001b[0mintegerStr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'0'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     20\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m         \u001b[0mintegerStr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mintegerPart\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minteger\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mbase\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     22\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     23\u001b[0m         \u001b[0mdecimalStr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'0'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'integerPart' is not defined"
     ]
    }
   ],
   "source": [
    "# num 要转化的数\n",
    "# base 进制\n",
    "# digit 保留的小数位数\n",
    "def convert(num,base,digit):\n",
    "    # 对参数进行检验\n",
    "    if num < 0:\n",
    "        print('暂不支持负数转化')\n",
    "        return\n",
    "    if (base < 2 and base >16) or not isinstance(base,int):\n",
    "        print('进制必须为大于等于2且小于等于16的整数')\n",
    "        return\n",
    "    if digit < 1 or not isinstance(digit,int):\n",
    "        print('位数必须为大于等于1的整数')\n",
    "        return\n",
    "    # 分为整数部分和小数部分分别计算\n",
    "    integer = int(num)\n",
    "    decimal = num - integer\n",
    "    if integer==0:\n",
    "        integerStr = '0'\n",
    "    else:\n",
    "        integerStr = integerPart(integer,base)\n",
    "    if decimal==0:\n",
    "        decimalStr = '0'\n",
    "    else:\n",
    "        decimalStr = decimalPart(decimal,base,digit)\n",
    "    return integerStr + \".\" + decimalStr\n",
    "\n",
    "convert(4,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(1.222,int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "288.0"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "# 生成后缀表达式\n",
    "def toSuffix(expression):\n",
    "    # 检验表达式\n",
    "    if len(re.sub(\"\\d|\\+|\\-|\\/|\\*|\\(|\\)|\\.\",\"\",expression))> 0:\n",
    "        raise Exception(\"表达式中含有非法字符\")\n",
    "    if expression.count('(')!=expression.count(')'):\n",
    "        raise Exception(\"括号不匹配\")\n",
    "    \n",
    "    # 用正则表达式匹配出数字和运算符让带小数点的数字在运算过程中不出错\n",
    "    strList = re.compile(\"\\d+\\.\\d+|\\+|\\-|\\/|\\*|\\(|\\)|\\d+\").findall(expression)\n",
    "    \n",
    "    # 定义符号优先级\n",
    "    degree = {\n",
    "        ')':0,\n",
    "        '(':0,\n",
    "        '*':2,\n",
    "        '/':2,\n",
    "        '+':1,\n",
    "        '-':1\n",
    "    }\n",
    "    stack = []\n",
    "    suffix = []\n",
    "    for s in strList:\n",
    "        # 如果替换掉'.'之后全为数字则加入后缀列表\n",
    "        if s.replace(\".\", \"\").isdigit():\n",
    "            suffix.append(s)\n",
    "        # 如果列为空或s为（或栈顶的优先级小于s，s入栈\n",
    "        elif len(stack)==0 or s == '(' or degree[stack[-1]]<degree[s]:\n",
    "            stack.append(s)\n",
    "        # 如果s为）弹栈直到（\n",
    "        elif s == ')':\n",
    "            while stack[-1] != '(':\n",
    "                suffix.append(stack.pop())\n",
    "            stack.pop()\n",
    "        # 如果栈顶不为（且栈顶的优先级大于等于s的优先级 弹栈并加入后缀列表\n",
    "        else:\n",
    "            while  stack[-1] != '(' and degree[stack[-1]]>=degree[s]:\n",
    "                suffix.append(stack.pop())\n",
    "            stack.append(s)\n",
    "    # 把剩余的字符串都弹栈并加入后缀列表\n",
    "    while len(stack)!=0:\n",
    "        suffix.append(stack.pop())\n",
    "    return suffix\n",
    "\n",
    "# 计算后缀表达式\n",
    "def compute(suffix):\n",
    "    stack = []\n",
    "    for s in suffix:\n",
    "        # 如果替换掉'.'之后全为数字则就入栈\n",
    "        if s.replace(\".\", \"\").isdigit():\n",
    "            stack.append(s)\n",
    "        elif s== '+':\n",
    "            tmp=float(stack.pop())+float(stack.pop())\n",
    "            stack.append(tmp)\n",
    "        elif s=='*':\n",
    "            tmp=float(stack.pop())*float(stack.pop())\n",
    "            stack.append(tmp)\n",
    "        elif s=='-':\n",
    "            tmp=float(stack.pop(-2))-float(stack.pop())\n",
    "            stack.append(tmp)\n",
    "        elif s=='/':\n",
    "            tmp=float(stack.pop(-2))/float(stack.pop())\n",
    "            stack.append(tmp)\n",
    "    return stack[0]\n",
    "\n",
    "\n",
    "def calculate(expression):\n",
    "    # 捕获异常并处理\n",
    "    try:\n",
    "        suffix=toSuffix(expression)\n",
    "        result=compute(suffix)\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        return\n",
    "    return result\n",
    "\n",
    "calculate(\"6*(5+(2+3)*8+3)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'calculate' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-87c01dd2b839>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mcalculate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"6*(5+(2+3)*8+3)\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'calculate' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "calculate(\"6*(5+(2+3)*8+3)\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['6.66', '*', '(', '5.33', '+', '(', '2', '+', '3', ')', '*', '8', '+', '3', ')']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "reobject = re.compile(\"\\d+\\.\\d+|\\+|\\-|\\/|\\*|\\(|\\)|\\d+\")\n",
    "t=reobject.findall(\"6.66*(5.33+(2+3)*8+3)\")\n",
    "print(t)\n",
    "s = \"6*(5+(2+3)*8+3)\"\n",
    "re.split(\"\\d+\\.\\d+|\\+|\\-|\\/|\\*|\\d+\",s)\n",
    "re.sub(\"()\\+|\\-|\\/|\\*|\\(|\\)()\",\" \",s)\n",
    "\n",
    "len(re.sub(\"\\d|\\+|\\-|\\/|\\*|\\(|\\)|\\.\",\"\",s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thwas was string example....wow!!!\n",
      "this is string example....wow!!!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'F'"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str = \"this is string example....wow!!!\"\n",
    "print (str.replace(\"is\", \"was\", 3))\n",
    "print(str)\n",
    "strList = ['A','B','C','D','E','F']\n",
    "strList.pop(-2)\n",
    "strList.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "# 改进版本\n",
    "# 生成后缀表达式\n",
    "def Suffix(expression):\n",
    "    # 检验表达式\n",
    "    if len(re.sub(\"\\d|\\+|\\-|\\/|\\*|\\(|\\)|\\.\",\"\",expression))> 0:\n",
    "        raise Exception(\"表达式中含有非法字符\")\n",
    "    if expression.count('(')!=expression.count(')'):\n",
    "        raise Exception(\"括号不匹配\")\n",
    "    \n",
    "    # 用正则表达式匹配出数字和运算符让带小数点的数字在运算过程中不出错\n",
    "    strList = re.compile(\"\\d+\\.\\d+|\\+|\\-|\\/|\\*|\\(|\\)|\\d+\").findall(expression)\n",
    "    \n",
    "    # 定义符号优先级\n",
    "    degree = {\n",
    "        ')':0,\n",
    "        '(':0,\n",
    "        '*':2,\n",
    "        '/':2,\n",
    "        '+':1,\n",
    "        '-':1\n",
    "    }\n",
    "    stack = []\n",
    "    suffix = []\n",
    "    for s in strList:\n",
    "        # 如果替换掉'.'之后全为数字则加入后缀列表\n",
    "        if s.replace(\".\", \"\").isdigit():\n",
    "            suffix.append(s)\n",
    "        # 如果列为空或s为（或栈顶的优先级小于s，s入栈\n",
    "        if len(stack)==0 or s == '(' or degree[stack[-1]]<degree[s]:\n",
    "            stack.append(s)\n",
    "        # 如果s为）弹栈直到（\n",
    "        if s == ')':\n",
    "            while stack[-1] != '(':\n",
    "                suffix.append(stack.pop())\n",
    "            stack.pop()\n",
    "        # 如果栈顶不为（且栈顶的优先级大于等于s的优先级 弹栈并加入后缀列表\n",
    "        else:\n",
    "            while  stack[-1] != '(' and degree[stack[-1]]>=degree[s]:\n",
    "                suffix.append(stack.pop())\n",
    "            stack.append(s)\n",
    "    # 把剩余的字符串都弹栈并加入后缀列表\n",
    "    while len(stack)!=0:\n",
    "        suffix.append(stack.pop())\n",
    "    return suffix\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf20",
   "language": "python",
   "name": "tf20"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
